//
//  HNSH_ChatSingleViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/17.
//

#import "HNSH_ChatSingleViewController.h"

//view
#import "HNSH_ChatSingleNavView.h"
#import "HNSH_TextMessageCell.h"
#import "HNSH_VoiceMessageCell.h"
#import "HNSH_HQVoiceMessageCell.h"
#import "HNSH_ImageMessageCell.h"
#import "HNSH_FileMessageCell.h"
#import "HNSH_GIFMessageCell.h"

//vc
#import "HNSH_ChatFriendDetailViewController.h"

@interface HNSH_ChatSingleViewController ()

/* topNavView */
@property (nonatomic, strong) HNSH_ChatSingleNavView *topNavView;
/* userInfo */
@property (nonatomic, strong) RCUserInfo *userInfo;

@end

@implementation HNSH_ChatSingleViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
}

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
    
    [self addAllViews];
        
    [self registerCell];
    
    [self loadUserInfo];
}

- (void)addAllViews{
    self.conversationMessageCollectionView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight - kBottomSafeHeight - 49);
    [self.view addSubview:self.topNavView];
    
    //
    [self.chatSessionInputBarControl.pluginBoardView updateItemWithTag:1001 normalImage:[UIImage imageNamed:@"chat_pic"] highlightedImage:[UIImage imageNamed:@"chat_pic"] title:@"相册"];
    [self.chatSessionInputBarControl.pluginBoardView updateItemWithTag:1002 normalImage:[UIImage imageNamed:@"chat_xj"] highlightedImage:[UIImage imageNamed:@"chat_xj"] title:@"拍摄"];
    [self.chatSessionInputBarControl.pluginBoardView removeItemWithTag:1003];

}

- (void)pluginBoardView:(RCPluginBoardView *)pluginBoardView clickedItemWithTag:(NSInteger)tag{
    [super pluginBoardView:pluginBoardView clickedItemWithTag:tag];

}

//MARK: Custom Methods
- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)report{
    HNSH_ChatFriendDetailViewController * vc = [HNSH_ChatFriendDetailViewController new];
    vc.rcUserId = self.targetId;
    [self.navigationController pushViewController:vc animated:YES];
}

/** 注册自定义cell */
- (void)registerCell{
    [self registerClass:HNSH_TextMessageCell.class
        forMessageClass:RCTextMessage.class];
    [self registerClass:HNSH_VoiceMessageCell.class forMessageClass:RCVoiceMessage.class];
    [self registerClass:HNSH_HQVoiceMessageCell.class forMessageClass:RCHQVoiceMessage.class];
    [self registerClass:HNSH_ImageMessageCell.class forMessageClass:RCImageMessage.class];
    [self registerClass:HNSH_FileMessageCell.class
        forMessageClass:RCFileMessage.class];
    [self registerClass:HNSH_GIFMessageCell.class
        forMessageClass:RCGIFMessage.class];
}


#pragma mark 修改文本消息中的超链接的颜色
- (void)willDisplayMessageCell:(RCMessageBaseCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    //修改头像
    if ([cell isKindOfClass:[RCMessageCell class]]) {
        RCMessageCell *msgCell = (RCMessageCell *)cell;
        UIImageView *portraitImageView= (UIImageView *)msgCell.portraitImageView;
        portraitImageView.image = [UIImage imageNamed:@"base_user_header_icon"];
    }
    //修改字体颜色
    if([cell isKindOfClass:[RCTextMessageCell class]]){
        RCTextMessageCell *txtCell = (RCTextMessageCell *)cell;
        txtCell.textLabel.attributeDictionary = @{@(NSTextCheckingTypeLink) :
                                                      @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#576B94"]},
                                               @(NSTextCheckingTypePhoneNumber) :
                                                      @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#576B94"]}};
        txtCell.textLabel.highlightedAttributeDictionary = @{@(NSTextCheckingTypeLink) :
                                                                 @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#576B94"]},
                                                         @(NSTextCheckingTypePhoneNumber) :
                                                                 @{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#576B94"]}};
    }
}

- (void)loadUserInfo{
    NSDictionary * params = @{@"ids":@[self.targetId]};
    [HNSH_ChatNetServer requestRongCloudUserInfoListWithParams:params success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            NSArray * array = responseObject.resultset;
            if (array.count) {
                NSDictionary * user = array.firstObject;
                RCUserInfo * userInfo = [[RCUserInfo alloc] init];
                userInfo.userId = user[@"id"];
                NSString * displayName = user[@"display_name"];
                userInfo.name = displayName.length ? displayName : user[@"name"];
                userInfo.portraitUri = user[@"portrait"];
                userInfo.extra = user[@"extra"];
                self.topNavView.imageString = userInfo.portraitUri;
                self.userInfo = userInfo;
                [[RCIM sharedRCIM] refreshUserInfoCache:userInfo withUserId:self.targetId];
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
    
}

#pragma mark - 用户点击回调
/** 点击头像 */
- (void)didTapCellPortrait:(NSString *)userId{
    if (![userId isEqualToString:[ST_UserManager shareInstance].rcUser.uid]) {
        HNSH_ChatFriendDetailViewController * vc = [HNSH_ChatFriendDetailViewController new];
        vc.rcUserId = userId;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didTapMessageCell:(RCMessageModel *)model{
    [super didTapMessageCell:model];

}



- (RCMessageContent *)willSendMessage:(RCMessageContent *)messageContent{
    NSString * extra = [NSString getExtraString];
    RCUserInfo * userInfo = [[RCUserInfo alloc] init];
    userInfo.userId = [ST_UserManager shareInstance].rcUser.uid;
    userInfo.name = [ST_UserManager shareInstance].rcUser.name;
    userInfo.portraitUri = [ST_UserManager shareInstance].rcUser.portrait;
    userInfo.extra = extra;
    messageContent.senderUserInfo = userInfo;
    return messageContent;
}

//- (void)inputTextView:(UITextView *)inputTextView
//    shouldChangeTextInRange:(NSRange)range
//      replacementText:(NSString *)text{
//    if (inputTextView.text.length > 100) {
//        inputTextView.text = [inputTextView.text substringToIndex:100];
//    }
//}

//MARK: Cycle Methods
- (HNSH_ChatSingleNavView *)topNavView{
    if (!_topNavView) {
        _topNavView = [[HNSH_ChatSingleNavView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarAndStatusBarHeight)];
        _topNavView.title = self.title;
        WeakSelf(weakSelf);
        _topNavView.backBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        _topNavView.reportBlock = ^{
            [weakSelf report];
        };
    }
    return _topNavView;
}

@end
